package com.problem;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 */
public class LengthOfLongestSubstring {

    /**
     * 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
     *
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {


        int n = 0;
        Set<Character> set = new HashSet<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length -n; i++) {
            for (int j = i; j < chars.length; j++) {
                if (!set.add(chars[j])) {
                    break;
                }
            }
            //不要放在break上面！
            n = n < set.size() ? set.size() : n;
            set.clear();
        }
        return n;
    }

    public static void main(String[] args) {
        LengthOfLongestSubstring length = new LengthOfLongestSubstring();
        String s = " ";
        System.out.println(length.lengthOfLongestSubstring(s));
    }
}
